
/**
 * created on 2012-03-21
 * @author  qiujz
 * @version 1.0
 *
 * Copyright (c)  Founder Software (Suzhou) Co., Ltd.  2012
 *  All rights reserved.
 *
 * 
 * ================================================================
 * Change Revision
 * ---------------------------------------------------------------
 *    DATE                BY                   COMMENT
 * ---------------------------------------------------------------
 * 2012-03-21         qiujz                Create class
 * 
 *
 * ================================================================
 */

package com.founder.bm.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.founder.mh.framework.entity.QueryResultBase;
import com.founder.mh.framework.util.BuildTableUtil;
import com.founder.bm.dao.IEnrollDtlDao;
import com.founder.bm.entity.EnrollDtlQueryParameter;
import com.founder.bm.entity.EnrollDtlTo;
import com.founder.bm.service.IEnrollDtlService;
import com.founder.mh.framework.entity.Sort;
import com.founder.mh.framework.service.impl.BaseServiceImpl;

@WebService(endpointInterface = "com.founder.bm.service.IEnrollDtlService")
public class EnrollDtlServiceImpl extends BaseServiceImpl implements IEnrollDtlService {

	private static final Log LOGGER = LogFactory.getLog(EnrollDtlServiceImpl.class);

	private IEnrollDtlDao enrollDtlDao;
	
	/**
	 * @return Dao接口
	 */
	public IEnrollDtlDao getEnrollDtlDao() {
		return enrollDtlDao;
	}
	
	/**
	 * @param enrollDtlDao
	 *            Dao接口
	 */
	public void setEnrollDtlDao(IEnrollDtlDao enrollDtlDao) {
		this.enrollDtlDao = enrollDtlDao;
	}
	
	/** 
	 * @inheritDoc
	 * @see com.founder.bm.service.IEnrollDtlService
	 *      #searchEnrollDtlList(com.founder.bm.entity.EnrollDtlQueryParameter)
	 */
	public QueryResultBase searchEnrollDtlList(EnrollDtlQueryParameter paramter) {
		if (paramter == null) {
			throw new RuntimeException("查询参数对象不能为空!");
		}
		
		// 放置查询结果的对象
		QueryResultBase rtnResult = new QueryResultBase();

		// 设置查询参数
		Map<String, Object> paramMap = new HashMap<String, Object>();
		// ToDO Add parameter
		//paramMap.put(BuildTableUtil
		//					.buildPropertyNameFromColumn(EnrollDtlTo.NAME), paramter.getName());

		List<EnrollDtlTo> enrollDtlList = null;
		int total = enrollDtlDao.getEnrollDtlCount(paramMap);
		rtnResult.setTotal(total);
		if (total == 0) {
			return rtnResult;
		}
		
		// 设置排序字段
		List<Sort> sortList = paramter.getSort();
		if (sortList != null && sortList.size() > 0) {
			StringBuilder sb = new StringBuilder();
			for (Sort sort : sortList) {
				sb.append(sort.getColumnName()).append(" ").append(
						sort.getOrder()).append(",");
			}

			paramMap.put("sort", sb.toString()
					.substring(0, sb.lastIndexOf(",")));

			if (LOGGER.isDebugEnabled()) {
				LOGGER.debug(paramMap.get("sort"));
			}
		}
		
		// 不分页的情况
		if (paramter.getStartRow() <= 0) {
			enrollDtlList= enrollDtlDao.searchEnrollDtlList(paramMap);
		} else {
			// 执行分页询
			int endRow = paramter.getStartRow() + paramter.getLimit() - 1;
			enrollDtlList = enrollDtlDao.searchEnrollDtlPagination(paramMap,
					paramter.getStartRow(), endRow);
		}
		rtnResult.setPageData(enrollDtlList);
		return rtnResult;
	}
	
	
	/** 
	 * @inheritDoc
	 * @see com.founder.bm.service.IEnrollDtlService
	 *      #getEnrollDtlByPrimaryKey(String)
	 */
	public EnrollDtlTo getEnrollDtlByPrimaryKey(String dtlId) {
		return enrollDtlDao.getEnrollDtlByPrimaryKey(dtlId);
	}
	
	
	/** 
	 * @inheritDoc
	 * @see com.founder.bm.service.IEnrollDtlService
	 *      #insertEnrollDtl(com.founder.bm.entityEnrollDtl.EnrollDtlTo)
	 */
	public EnrollDtlTo insertEnrollDtl(EnrollDtlTo enrollDtl) {
		
			 enrollDtlDao.insertEnrollDtl(enrollDtl);
			 //enrollDtl = enrollDtlDao.getEnrollDtlByPrimaryKey(dtlId);
		
		
		return enrollDtl;
	}
	
	/** 
	 * @inheritDoc
	 * @see com.founder.bm.service.IEnrollDtlService
	 *      #updateEnrollDtl(com.founder.bm.entity.EnrollDtlTo)
	 */
	public int updateEnrollDtl(EnrollDtlTo enrollDtl) {
		return enrollDtlDao.updateEnrollDtlByBean(enrollDtl);		
	}
	
	
	/** 
	 * @inheritDoc
	 * @see com.founder.bm.service.IEnrollDtlService
	 *      #deleteEnrollDtl(String)
	 */
	public int deleteEnrollDtl(String dtlId) {
		return enrollDtlDao.deleteEnrollDtlByPrimaryKey(dtlId);
	}
	
	/** 
	 * @inheritDoc
	 * @see com.founder.bm.service.IEnrollDtlService
	 *      #deleteEnrollDtlBatch(String[])
	 */
	public void deleteEnrollDtlBatch(String[] keyArray) {
		enrollDtlDao.deleteEnrollDtlBatch(keyArray);
	}
	
}
	
